package com.app.aipass.activity.order;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.ObservableField;

import com.app.aipass.R;
import com.app.aipass.app.AppApplication;
import com.app.aipass.entity.TransactionPlanEntity;
import com.app.aipass.fragment.guadandetail.GuaDanDetailFragment;
import com.app.aipass.fragment.orderdetail.OrderDetailFragment;
import com.app.aipass.utils.TimeUtil;
import com.app.aipass.utils.Utils;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

/**
 * @author: woking
 * @date: 2021/6/7
 * @description：
 */
public class OrderRecordTabItemViewModel extends ItemViewModel<OrderRecordTabViewModel> {
    public ObservableField<TransactionPlanEntity> entity = new ObservableField<>();
    public Drawable drawableImg;

    public OrderRecordTabItemViewModel(@NonNull OrderRecordTabViewModel viewModel, TransactionPlanEntity entity) {
        super(viewModel);
        this.entity.set(entity);
        //ImageView的占位图片，可以解决RecyclerView中图片错误问题
        drawableImg = ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.ic_launcher);
    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
    public int getPosition() {
        return viewModel.getItemPosition(this);
    }

    public String setType()
    {
        if(entity.get().type == 1)//购买
        {
            return "求购";
        }
        else
        {
            return "转赠";
        }
    }

    public int isGoneNor()
    {
        int status = View.VISIBLE;
        if(entity.get().isOrder)//订单
        {
            //订单状态:0=待支付,1=待确认,2=确认成功,3=订单关闭,4=仲裁 5 待审核
            switch (entity.get().status)
            {

            }
        }
        else
        {
            switch (entity.get().status)
            {
                case 0:
                    status = View.GONE;
                    break;
            }
        }
        return status;
    }
    public int isGoneJinXingZhong()
    {
        int status = View.GONE;
        if(entity.get().isOrder)//订单
        {
            //订单状态:0=待支付,1=待确认,2=确认成功,3=订单关闭,4=仲裁 5 待审核
            switch (entity.get().status)
            {

            }
        }
        else
        {
            switch (entity.get().status)
            {
                case 0:
                    status = View.VISIBLE;
                    break;
            }
        }
        return status;
    }

    public String setStatus()
    {
        String status = "完成";
        if(entity.get().isOrder)//订单
        {
            //订单状态:0=待支付,1=待确认,2=确认成功,3=订单关闭,4=仲裁 5 待审核
            switch (entity.get().status)
            {
                case 0: status = "待付款"; break;
                case 1: status = "待放行"; break;
                case 2: status = "已完成"; break;
                case 3: status = "已取消"; break;
                case 4: status = "异常"; break;
                case 5: status = "待审核"; break;
            }
        }
        else
        {
            switch (entity.get().status)
            {
                case 0:
                    status = "进行中";

                break;
                case 1: status = "已完成"; break;
                case 2: status = "已取消"; break;
                case 3: status = "待审核"; break;
            }
        }
        return status;
    }


    public String setTime()
    {
        if(entity.get().isOrder)//订单
        {
//            Log.e("TAG",entity.get().createtime + "============setTime========length===="+entity.get().createtime.length());
            if (entity.get().createtime.length() == 10) {
                return TimeUtil.formatTtimeName(Long.parseLong(entity.get().createtime));
            }
            return entity.get().createtime;
        }
        else
        {
            return entity.get().createtime;
        }
    }

    public String setPriceType()
    {
//        if(!entity.get().isOrder)
//        {
            return "单价(" + AppApplication.pricetag +  ")";
//        }
//        else
//        {
//            return "单价(CSC)";
////        }
    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if(!Utils.isLogin())
            {
                return;
            }
            //订单
            if(entity.get().isOrder) {
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("entity", entity.get());
                mBundle.putBoolean("isOrder", entity.get().isOrder);
                viewModel.startContainerActivity(OrderDetailFragment.class.getCanonicalName(), mBundle);
            }
            else {
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("entity", entity.get());
                mBundle.putBoolean("isOrder", entity.get().isOrder);
                viewModel.startContainerActivity(GuaDanDetailFragment.class.getCanonicalName(), mBundle);
            }
        }
    });
    //条目的长按事件
    public BindingCommand itemLongClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //以前是使用Messenger发送事件，在NetWorkViewModel中完成删除逻辑
//            Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
            //现在ItemViewModel中存在ViewModel引用，可以直接拿到LiveData去做删除
//            ToastUtils.showShort(entity.get().getName());
        }
    });
//    /**
//     * 可以在xml中使用binding:currentView="@{viewModel.titleTextView}" 拿到这个控件的引用, 但是强烈不推荐这样做，避免内存泄漏
//     **/
//    private TextView tv;
//    //将标题TextView控件回调到ViewModel中
//    public BindingCommand<TextView> titleTextView = new BindingCommand(new BindingConsumer<TextView>() {
//        @Override
//        public void call(TextView tv) {
//            NetWorkItemViewModel.this.tv = tv;
//        }
//    });
}
